/* eslint-disable camelcase */
/* eslint-disable no-debugger */
import { ApiArticles, ApiResponse, Channel } from '@/types/data'
import { RootThunkAction } from '@/types/store'
import request from '@/utils/request'
import { hasToken } from '@/utils/storage'
import { getChannels, setChannels } from '@/utils/storageChannel'

// 获取用户频道信息
// 1. 登录用户
//    正常发请求
// 2. 没有登录，从本地取频道
//       本地没有频道，求一次，保存到本地
//       本地有频道，直接用
export const getUserChannel = (): RootThunkAction => {
  return async (dispatch) => {
    if (hasToken()) {
      //   Token:本接口返回值的类型
      const res = await request.get <ApiResponse<{channels: Channel[]}>>('/user/channels')

      dispatch({
        type: 'home/getUserChannel',
        payload: res.data.data.channels
      })
    } else {
      const channels = getChannels()
      if (channels.length === 0) {
        const res = await request.get <ApiResponse<{channels: Channel[]}>>('/user/channels')
        // // 保存redux
        // redux
        dispatch({
          type: 'home/getUserChannel',
          payload: res.data.data.channels
        })
        setChannels(res.data.data.channels)
      } else {
        // 把localstorage中的频道直接保存到redux
        dispatch({
          type: 'home/getUserChannel',
          payload: channels
        })
      }
    }
  }
}

// 添加用户频道信息
// 1. 登录用户
//    正常发请求，调用接口做添加
// 2. 没有登录，从本地取频道，做添加
export const addChannel = (channel: Channel): RootThunkAction => {
  return async (dispatch, getState) => {
    const channels = getState().home.userChannel
    if (hasToken()) {
      //   Token:本接口返回值的类型 <ApiResponse<{channels: Channel[]}>>
      await request.patch('/user/channels', {
        channels: [channel]
      })

      // dispatch({
      //   type: 'home/getUserChannel',
      //   payload: res.data.data.channels
      // })
    } else {
      // 没有登录，本地localstorage要做保存
      setChannels([...channels, channel])
    }

    // 保存到redux -----> 视图就会更新
    dispatch({
      type: 'home/getUserChannel',
      payload: [...channels, channel]
    })
  }
}

// 删除用户频道信息
// 1. 登录用户
//    正常发请求，调用接口做删除
// 2. 没有登录，从本地取频道，做删除
export const delChannel = (id: string): RootThunkAction => {
  return async (dispatch, getState) => {
    const channels = getState().home.userChannel
    if (hasToken()) {
      //   Token:本接口返回值的类型 <ApiResponse<{channels: Channel[]}>>
      await request.delete('/user/channels/' + id)
    } else {
      // 没有登录，本地localstorage要做删除
      setChannels(channels.filter(it => it.id !== id))
    }

    // 保存到redux -----> 视图就会更新
    dispatch({
      type: 'home/getUserChannel',
      payload: channels.filter(it => it.id !== id)
    })
  }
}

/**
 * 切换高亮频道
 * @param key
 * @returns
 */
export const changeChannel = (key: string): RootThunkAction => {
  return async (dispatch) => {
    await dispatch({
      type: 'home/changeChannel',
      id: key
    })
  }
}

// 获取全部的频道信息
export const getAllChannel = (): RootThunkAction => {
  return async (dispatch) => {
    //   Token:本接口返回值的类型
    const res = await request.get <ApiResponse<{channels: Channel[]}>>('/channels')
    dispatch({
      type: 'home/getAllChannel',
      payload: res.data.data.channels
    })
  }
}
/**
 * 获取文章列表
 */
export const getArticleList = (channel_id:string, timestamp:string = Date.now() + '', isFlush:boolean = false):RootThunkAction => {
  return async (dispatch) => {
    const res = await request.get<ApiResponse<{pre_timestamp:string, results:ApiArticles[]}>>('/articles', {
      params: {
        channel_id,
        timestamp
      }
    })
    if (isFlush) {
      dispatch({ type: 'home/booleanArticles', payload: { ...res.data.data, channel_id } })
    } else {
      dispatch({ type: 'home/getArticles', payload: { ...res.data.data, channel_id } })
    }
  }
}
